package com.tejava.cafe_in;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;



import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Tab04_Notice extends ListActivity {

	
    
	
    
    
	URL url;
//	String[] title = {"1","2","3"};
//	String[] date = {"a", "b", "c"};
//	String[] contents = {"d", "e", "f"};
	
	String[] title;
	String[] date;
	String[] contents;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab04__notice);
		ArrayList<String> m_orders = new ArrayList<String>();
		doXmlParse();
		
		//m_orders.add("1");
		//m_orders.add("2");
		//m_orders.add("3");
		 for(int i = 0; i < title.length; i++){
			 m_orders.add(title[i]);
	        }

		PersonAdapter m_adapter = new PersonAdapter(this, R.layout.activity_tab04__notice,
				m_orders);
		setListAdapter(m_adapter);
		

	}
	
	

	

	private class PersonAdapter extends ArrayAdapter<String> {

		private ArrayList<String> items;

		public PersonAdapter(Context context, int textViewResourceId,
				ArrayList<String> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.custom_notice_view, null);
			}
			String p = items.get(position);
			if (p != null) {
				TextView tt = (TextView) v.findViewById(R.id.titleString1);
				TextView bt = (TextView) v.findViewById(R.id.titleString2);
				if (tt != null) {
					tt.setText(title[position]);
				}
				if (bt != null) {
					bt.setText(contents[position]);
				}
			}
			return v;
		}
	}


	class Person {

		private String Name;
		private String Number;

		public Person(String _Name, String _Number) {
			this.Name = _Name;
			this.Number = _Number;
		}

		public String getName() {
			return Name;
		}

		public String getNumber() {
			return Number;
		}

	}

	private void doXmlParse() {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		
		try{
			URL text= new URL("http://webpds.cafe24.com/cafeIn/notice/showNotice.php"); 			
			
			
			
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser=parserCreator.newPullParser();
			
			parser.setInput(text.openStream(),null);
			
			Log.i("NET", "Parsing...");
			
			// ÀÐ¾î¿Â Á¤º¸¸¦ ÆÄ½ÌÇÏ¿© µ¥ÀÌÅÍ¸¦ ¸¸µç´Ù.
			int total = 1;
			int i = 0 ;
			int j = 0;
			int k = 0;
			
			int parseEvent = parser.getEventType();
			while(parseEvent != XmlPullParser.END_DOCUMENT){
				
				switch(parseEvent){
				
				
				case XmlPullParser.START_TAG:
					String tag=parser.getName();
					if(tag.compareTo("total")==0){
						total=Integer.parseInt(parser.nextText());
						
						title = new String[total];
						contents = new String[total];
						date = new String[total];
						
					}
					
					if(tag.compareTo("Title")==0)
					{
						title[i]=parser.nextText();
						i++;
						Log.i("NET","START...");
					}
					if(tag.compareTo("Content")==0)
					{
						contents[j]=parser.nextText();
						j++;
					}
					if(tag.compareTo("Created")==0)
					{
						date[k]=parser.nextText();
						k++;
					}
					
					
					
					break;
				}
				parseEvent=parser.next();
				// ´ÙÀ½ µ¥ÀÌÅÍ·Î ³Ñ¾î°£´Ù.. END_DOCUMENTÀÏ¶§±îÁö..
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
			//st++;
		
//		Log.i("NET","End...");

		}

	/*
	
		
		
		try {

			
			url = new URL("http://webpds.cafe24.com/cafeIn/notice/showNotice.php");

			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				
				InputStream in = httpConnection.getInputStream();

				DocumentBuilderFactory dbf;
				dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();

				// parsing
				Document dom = db.parse(in);
				// get element
				Element root = dom.getDocumentElement();
				

				Toast.makeText(this,  root.getNodeName(), Toast.LENGTH_SHORT).show();
				// get a list
				NodeList thumbs = root.getElementsByTagName("item");
				// for performance
				int length = thumbs.getLength();
//				Toast.makeText(Notice.this, length+"", 5000).show();
				
				title = new String[length];
				contents = new String[length];
				date = new String[length];

				System.out.println("title : " + length);
				// xml is avail
				if (thumbs != null && length > 0) {
					for (int i = 0; i < length; i++) {
						Element thumb = (Element) thumbs.item(i);

						Element t1 = (Element) thumb.getElementsByTagName(
								"Title").item(0);
						Element t2 = (Element) thumb.getElementsByTagName(
								"Content").item(0);
						Element t3 = (Element) thumb.getElementsByTagName(
								"Created").item(0);

						String titles = t1.getFirstChild().getNodeValue();
						String contentss = t2.getFirstChild().getNodeValue();
						String dates = t3.getFirstChild().getNodeValue();
						title[i] = titles;
						contents[i] = contentss;
						date[i] = dates;
						if (title[i] == null) {
							title[i] = titles;
						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		

	}
*/
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		FrameLayout f = (FrameLayout) v.findViewById(R.id.frame1);
//		String tmp = "f Num"+f.getVisibility();
//		Toast.makeText(this,  tmp, Toast.LENGTH_SHORT).show();

		if (f.getVisibility() == 8) {
			f.setVisibility(View.VISIBLE);
		} else if (f.getVisibility() == 0) {
			f.setVisibility(View.GONE);
		}
		
		
		super.onListItemClick(l, v, position, id);
		
	
	}
	
	@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
            finish();
            return true; 
        
    }
    
}








